﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LTJ.Common;
using LTJ.Entity;
using LTJ.FrameWork;

namespace uswinweb.admin
{
    public partial class compmanage_update : AdminPage
    {
        public CompanyInfo compInfo = new CompanyInfo();
        public int compID = SASRequest.GetInt("cid", 0);
        protected void Page_Load(object sender, EventArgs e)
        {
            compInfo = Companys.GetCompanyByID(compID);
            if (SASRequest.IsPost())
            {
                UpdateComp();
            }
        }
        protected void UpdateComp()
        {

            string cname = SASRequest.GetFormString("cNametxt");
            string ename = SASRequest.GetFormString("eNametxt");
            string relater = SASRequest.GetFormString("relatertxt");
            string relateform = SASRequest.GetFormString("relateformtxt");
            string bio = SASRequest.GetFormString("biotxt");
            string welcome = SASRequest.GetFormString("welcometxt");
            string filename = FileUpload1.PostedFile.FileName;
            if (filename != "")
            {
                filename = System.IO.Path.GetFileName(filename);
                //改文件名
                int index = filename.LastIndexOf(".");
                string lastName = filename.Substring(index, filename.Length - index);
                Boolean fileOK = false;

                String path = Server.MapPath("~/upload/");

                string newfile = DateTime.Now.ToString("yyyyMMddhhmmss") + lastName;
                if (FileUpload1.HasFile)
                {
                    String fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
                    String[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg", ".bmp" };
                    for (int i = 0; i < allowedExtensions.Length; i++)
                    { if (fileExtension == allowedExtensions[i]) { fileOK = true; } }
                }
                if (fileOK)
                {
                    try
                    {
                        if (!System.IO.Directory.Exists(path))//判断文件夹是否已经存在
                        {
                            System.IO.Directory.CreateDirectory(path);//创建文件夹
                        }
                        FileUpload1.PostedFile.SaveAs(path + newfile);//上传文件到ipath这个路径里

                        if (compInfo.logo != "")
                        {
                            try
                            {
                                System.IO.File.Delete(path + compInfo.logo);
                            }
                            catch (Exception ee)
                            {

                            }
                        }
                        compInfo.cname = cname;
                        compInfo.ecname = ename;
                        compInfo.logo = newfile;
                        compInfo.relateform = relateform;
                        compInfo.relater = relater;
                        compInfo.welcome = welcome;
                        compInfo.status = 1;
                        compInfo.bio = bio;
                        Companys.UpdateCompany(compInfo);
                         base.RegisterStartupScript("PAGE", "parent.document.getElementById('reclick').click();parent.Lightview.hide();");
                    }
                    catch (Exception ex) { }
                    finally
                    {
                    }
                }
                else { }
            }
            else
            {
                compInfo.cname = cname;
                compInfo.ecname = ename;
                compInfo.relateform = relateform;
                compInfo.relater = relater;
                compInfo.welcome = welcome;
                compInfo.status = 1;
                compInfo.bio = bio;
                Companys.UpdateCompany(compInfo);
                base.RegisterStartupScript("PAGE", "parent.document.getElementById('reclick').click();parent.Lightview.hide();");
            }
        }

    }
}